package com.want.evenchat.controller.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.want.evenchat.R;
import com.want.evenchat.model.bean.InvitionInfo;
import com.want.evenchat.model.bean.UserInfo;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Want on 2018/5/7.
 */

public class InviteAdapter extends BaseAdapter{
    //邀请信息列表页面的适配器

    private Context mContext;

    private List<InvitionInfo> mInvitationInfos = new ArrayList<>();

    private OnInviteListener mOnInviteListener;

    private InvitionInfo invitionInfo;

    public InviteAdapter(Context context , OnInviteListener onInviteListener) {
        mContext = context;

        mOnInviteListener = onInviteListener;
    }

    public void refresh(List<InvitionInfo> invitionInfos){

        mInvitationInfos.clear();

        if (invitionInfos != null && invitionInfos.size() >= 0){
            mInvitationInfos.addAll(invitionInfos);

            notifyDataSetChanged();;
        }
    }

    @Override
    public int getCount() {
        return mInvitationInfos == null? 0 : mInvitationInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInvitationInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        //1.获取或创建ViewHolder
        ViewHolder holder = null;
        if (holder == null){
            holder = new ViewHolder();

            convertView = View.inflate(mContext , R.layout.item_invite , null);

            holder.name = convertView.findViewById(R.id.textview_invite_name);
            holder.reason = convertView.findViewById(R.id.textview_invite_reason);
            holder.accept = convertView.findViewById(R.id.button_invite_accept);
            holder.reject = convertView.findViewById(R.id.button_invite_reject);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        //2.获取当前item数据
        invitionInfo = mInvitationInfos.get(position);

        //3.显示当前item数据
        UserInfo user = invitionInfo.getUser();
        if (user != null){//说明当前是联系人的邀请信息
            //名称的展示
            holder.name.setText(invitionInfo.getUser().getName());

            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);

            //原因
            if (invitionInfo.getStatus() == InvitionInfo.InvitationStatus.NEW_INVITE){//状态是新的邀请

                if (invitionInfo.getReason() == null){
                    holder.reason.setText("添加好友");
                }else {
                    holder.reason.setText(invitionInfo.getReason());
                }

                holder.accept.setVisibility(View.VISIBLE);
                holder.reject.setVisibility(View.VISIBLE);

            }else if (invitionInfo.getStatus() == InvitionInfo.InvitationStatus.INVITE_ACCEPT){

                if (invitionInfo.getReason() == null){
                    holder.reason.setText("接受邀请");
                }else{
                    holder.reason.setText(invitionInfo.getReason());
                }

            }else  if (invitionInfo.getStatus() == InvitionInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){

                if (invitionInfo.getReason() == null){
                    holder.reason.setText("邀请被接受");
                }else {
                    holder.reason.setText(invitionInfo.getReason());
                }

            }

            //接受按钮的点击事件
            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onAccept(invitionInfo);
                }
            });

            //拒绝按钮的点击事件
            holder.reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onReject(invitionInfo);
                }
            });


        }else {//群组的邀请信息

            //显示名称
            holder.name.setText(invitionInfo.getGroup().getInvatePerson());

            //显示状态(原因)
            holder.accept.setVisibility(View.GONE);
            holder.reject.setVisibility(View.GONE);

            // 显示原因
            switch(invitionInfo.getStatus()){
                // 您的群申请已经被接受
                case GROUP_APPLICATION_ACCEPTED:
                    holder.reason.setText("您的群申请请已经被接受");
                    break;
                //  您的群邀请已经被接收
                case GROUP_INVITE_ACCEPTED:
                    holder.reason.setText("您的群邀请已经被接收");
                    break;

                // 你的群申请已经被拒绝
                case GROUP_APPLICATION_DECLINED:
                    holder.reason.setText("你的群申请已经被拒绝");
                    break;

                // 您的群邀请已经被拒绝
                case GROUP_INVITE_DECLINED:
                    holder.reason.setText("您的群邀请已经被拒绝");
                    break;

                // 您收到了群邀请
                case NEW_GROUP_INVITE:
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);

                    // 接受邀请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteAccept(invitionInfo);
                        }
                    });

                    // 拒绝邀请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteReject(invitionInfo);
                        }
                    });

                    holder.reason.setText("您收到了群邀请");
                    break;

                // 您收到了群申请
                case NEW_GROUP_APPLICATION:
                    holder.accept.setVisibility(View.VISIBLE);
                    holder.reject.setVisibility(View.VISIBLE);

                    // 接受申请
                    holder.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationAccept(invitionInfo);
                        }
                    });

                    // 拒绝申请
                    holder.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationReject(invitionInfo);
                        }
                    });

                    holder.reason.setText("您收到了群申请");
                    break;

                // 你接受了群邀请
                case GROUP_ACCEPT_INVITE:
                    holder.reason.setText("你接受了群邀请");
                    break;

                // 您批准了群申请
                case GROUP_ACCEPT_APPLICATION:
                    holder.reason.setText("您批准了群申请");
                    break;

                // 您拒绝了群邀请
                case GROUP_REJECT_INVITE:
                    holder.reason.setText("您拒绝了群邀请");
                    break;

                // 您拒绝了群申请
                case GROUP_REJECT_APPLICATION:
                    holder.reason.setText("您拒绝了群申请");
                    break;
            }
        }

        //4.返回View
        return convertView;
    }

    private class ViewHolder{
        private TextView name;
        private TextView reason;
        private Button accept;
        private Button reject;
    }

    public interface OnInviteListener{
        //联系人接受按钮的点击事件
        void onAccept(InvitionInfo invitionInfo);

        //联系人拒绝按钮的点击事件
        void onReject(InvitionInfo invitionInfo);

        //接受邀请按钮处理
        void onInviteAccept(InvitionInfo invitionInfo);
        //拒绝邀请按钮处理
        void onInviteReject(InvitionInfo invitionInfo);
        //接受申请按钮处理
        void onApplicationAccept(InvitionInfo invitionInfo);
        //拒绝申请按钮处理
        void onApplicationReject(InvitionInfo invitionInfo);

    }
}
